#!/bin/sh

####################################################################################################

function report()
{
	echo "##########################################################################################"
	echo "$1"
}

function abswd()
{
	abspath=$(cd ${0%/*} && echo $PWD/${0##*/})
	path_only=`dirname "$abspath"`
}

function dir_resolve()
{
	cd "$1" 2>/dev/null || return $? 
	echo "`pwd -P`" 
}

function build_for_leopard()
{
	export DEVSDK="/Developer/SDKs/MacOSX10.5.sdk"
	export SYSROOT="-isysroot ${DEVSDK} -mmacosx-version-min=10.5"
	export SUFFIX="-macosx10.5"
}

function build_for_snow_leopard()
{
	export DEVSDK="/Developer/SDKs/MacOSX10.6.sdk"
	export SYSROOT="-isysroot ${DEVSDK} -mmacosx-version-min=10.6"
	export SUFFIX="-macosx10.6"
}

####################################################################################################

export DRISHTI_VERSION=2.1.0
export ABSWD=`abswd`
export BUILD_PRODUCTS=${ABSWD}../..
export BUILD_PRODUCTS_DIR="`dir_resolve ${BUILD_PRODUCTS}/bin`"
export BUILD_PACKAGE_DIR="`dir_resolve tmp`"
export THIRD_PARTY_DIR="`dir_resolve ${BUILD_PRODUCTS}/3rdParty`"
export THIRD_PARTY_BUILD="`dir_resolve ${THIRD_PARTY_DIR}/build`"
export THIRD_PARTY_PREFIX="`dir_resolve ${THIRD_PARTY_DIR}`"
export FETCH="curl -f -L -O "
export INSTALL_DIR=`pwd`/..
export INSTALL_PREFIX="`dir_resolve $INSTALL_DIR`"
export THIRD_PARTY_QT_DIR=${THIRD_PARTY_PREFIX}/qt
# export QT_FRAMEWORK_LIST="QtCore QtGui QtOpenGL QtNetwork QtAssistant QtXml QtSvg QtHelp QtSql"
export QT_FRAMEWORK_LIST="QtCore QtDeclarative QtDesigner QtDesignerComponents QtGui QtHelp QtMultimedia QtNetwork QtOpenGL QtScript QtScriptTools QtSql QtSvg QtTest QtXml QtXmlPatterns"

####################################################################################################

function fetch() 
{
	url=$1
	tarball=$2
	dir=$3
	
	if [ ! -e $tarball ]; then
		report ""
		report "Fetching '$tarball'..."
		$FETCH $url > $tarball || exit -1
		report "Downloaded '$url'..."
	fi
	
	report ""
	report "Extracting '$tarball'..."

	if test -f ${tarball%.*}*.bz2 ; then
		tar jxf ${tarball} || exit -1
	elif  test -f ${tarball%.*}*.gz ; then
		tar zxf ${tarball} || exit -1
	elif  test -f ${tarball%.*}*.tgz ; then
		tar zxf ${tarball} || exit -1
	elif  test -f ${tarball%.*}*.zip ; then
		unzip -uo ${tarball} || exit -1
	fi

	report "Prepared '$tarball'!"
	report ""
}

####################################################################################################

# Give an error message and exit uncleanly.
function die
{
    echo "'$@' failed, bailing..."
    exit 1
}

# Run a command which must succeed, else the script terminates.
function try
{
    $@
    if test $? -ne 0 ; then
        die "$@"
    fi
}

####################################################################################################

function pkg_change () 
{
  if [ ! -x $1 ]
  then
    echo "pkg_change: Missing binary $1"
    exit -1
  fi

  echo "install_name_tool -change \n\t$2\n\t$3\n\t$1\n"
  install_name_tool -change $2 $3 $1 || exit -1
}

function pkg_rename () 
{
  if [ ! -x $1 ] 
  then
    echo "pkg_rename: Missing binary $1"
    exit -1
  fi

  echo "install_name_tool -id\n\t$2\n\t$1\n"
  install_name_tool -id $2 $1 || exit -1
}

####################################################################################################

function localise_dylib ()
{
	base=$1
	rsc=$2
	fw=$3
	lib=$4
	libdir=`dirname "${lib}"`
	libbase=`basename "${lib}"`
	
	report "Localising '${libbase}' dependency against '${fw}.framework' for ${base}.app..."

	echo "install_name_tool -change\n" \
		"${libdir}/${libbase}\n" \
		"@executable_path/../${rsc}/${libbase}\n" \
		"${base}.app/Contents/Frameworks/${fw}.framework/Versions/4/${fw}"
	
	install_name_tool -change \
		"${libdir}/${libbase}" \
		"@executable_path/../${rsc}/${libbase}" \
		"${base}.app/Contents/Frameworks/${fw}.framework/Versions/4/${fw}"	

	for dp in ${QT_FRAMEWORK_LIST}; do
		install_name_tool -change \
			"${THIRD_PARTY_QT_DIR}/lib/${dp}.framework/Versions/4/${dp}" \
			"@executable_path/../Frameworks/${dp}.framework/Versions/4/${dp}" \
			"${base}.app/Contents/${rsc}/${libbase}"
	done
}

function install_dylib ()
{
	base=$1
	rsc=$2
	lib=$3

	libdir=`dirname "${lib}"`
	libbase=`basename "${lib}"`
	
	
	report "Installing '${lib}' into '${base}.app'..."

	cp -v "${libdir}/${libbase}" "${base}.app/Contents/${rsc}/${libbase}" || exit -1

	echo "install_name_tool -id\n" \
		"@executable_path/../${rsc}/${libbase}\n" \
		"${base}.app/Contents/${rsc}/${libbase}"

	install_name_tool -id \
		"@executable_path/../${rsc}/${libbase}" \
		"${base}.app/Contents/${rsc}/${libbase}" || exit -1

	echo "install_name_tool -change\n" \
		"${libdir}/${libbase}\n" \
        "@executable_path/../${rsc}/${libbase}" \
		"${base}.app/Contents/MacOS/${base}"

	install_name_tool -change \
		"${libdir}/${libbase}" \
        "@executable_path/../${rsc}/${libbase}" \
		"${base}.app/Contents/MacOS/${base}"  || exit -1
}

####################################################################################################

function localise_qt_framework ()
{
	base=$1
	fw1=$2
	fw2=$3
	
	echo "\nLocalising '${fw1}.framework' against '${fw2}.framework' for '${base}.app'..."

	echo "install_name_tool -change\n" \
		"${THIRD_PARTY_QT_DIR}/lib/${fw2}.framework/Versions/4/${fw2}\n" \
		"@executable_path/../Frameworks/${fw2}.framework/Versions/4/${fw2}\n" \
		"${base}.app/Contents/Frameworks/${fw1}.framework/Versions/4/${fw1}"
	
	install_name_tool -change \
		"${THIRD_PARTY_QT_DIR}/lib/${fw2}.framework/Versions/4/${fw2}" \
		"@executable_path/../Frameworks/${fw2}.framework/Versions/4/${fw2}" \
		"${base}.app/Contents/Frameworks/${fw1}.framework/Versions/4/${fw1}"
	
}

function install_qt_framework ()
{
	base=$1
	fw=$2
	
	report "Installing '${fw}.framework' into '${base}.app'..."

	cp -R "${THIRD_PARTY_QT_DIR}/lib/${fw}.framework" "${base}.app/Contents/Frameworks/${fw}.framework" || exit -1

	echo "install_name_tool -id\n" \
		"@executable_path/../Frameworks/${fw}.framework/Versions/4/${fw}\n" \
		"${base}.app/Contents/Frameworks/${fw}.framework/Versions/4/${fw}"

	install_name_tool -id \
		"@executable_path/../Frameworks/${fw}.framework/Versions/4/${fw}" \
		"${base}.app/Contents/Frameworks/${fw}.framework/Versions/4/${fw}"  || exit -1

	echo "install_name_tool -change\n" \
		"${THIRD_PARTY_QT_DIR}/lib/${fw}.framework\n" \
        "@executable_path/../Frameworks/${fw}.framework/Versions/4/${fw}\n" \
		"${base}.app/Contents/MacOS/${base}"

	install_name_tool -change \
		"${THIRD_PARTY_QT_DIR}/lib/${fw}.framework/Versions/4/${fw}" \
        "@executable_path/../Frameworks/${fw}.framework/Versions/4/${fw}" \
		"${base}.app/Contents/MacOS/${base}" || exit -1
}

####################################################################################################

function install_qt_plugins ()
{
	base=$1
	APP_PLUGINS_DIR=${base}.app/Contents/plugins

	mkdir -p $APP_PLUGINS_DIR
	mkdir -p $APP_PLUGINS_DIR/imageformats
	
	for type in 'ico' 'jpeg' 'mng' 'svg' 'tiff'; do
		install_dylib $base "plugins/imageformats" "${THIRD_PARTY_QT_DIR}/plugins/imageformats/libq${type}.dylib" || exit 1
	  	
	  	for dp in ${QT_FRAMEWORK_LIST}; do
			localise_dylib $base "plugins/imageformats" $dp "$APP_PLUGINS_DIR/imageformats/libq${type}.dylib" || exit -1
		done
	done
}

function create_qt_conf ()
{
	base=$1
	
    if [ ! -d ${base}.app ] 
    then
	echo "Missing application dir $1"
	exit -1
    fi

    mkdir -p ${base}.app/Contents/Resources || exit -1
    qtconf=${base}.app/Contents/Resources/qt.conf
    
    echo "[Paths]" > $qtconf
    echo "Plugins = plugins" >> $qtconf
}

####################################################################################################

function create_universal_lib()
{
	libdir="$1"
	libname="$2"

	pushd $libdir || exit 1
	mkdir -p universal/lib || exit 1

	report ""
	report "Linking universal '${libname}'..."

	lipo -create \
			i686/lib/${libname} \
			x86_64/lib/${libname} \
			ppc/lib/${libname} \
		-output \
			universal/lib/${libname}|| exit 1

	cp -R i686/include universal || exit 1
	
	report "Created universal '${libname}'..."
	report ""

	popd
}

####################################################################################################

function create_intel_lib()
{
	libdir="$1"
	libname="$2"

	pushd $libdir || exit 1
	mkdir -p intel/lib || exit 1

	report ""
	report "Linking intel '${libname}'..."

	lipo -create \
			i686/lib/${libname} \
			x86_64/lib/${libname} \
		-output \
			intel/lib/${libname} || exit 1
			
	cp -R i686/include universal || exit 1

	report "Created intel '${libname}'..."
	report ""

	popd
}

####################################################################################################

function create_powerpc_lib()
{
	libdir="$1"
	libname="$2"

	pushd $libdir || exit 1
	mkdir -p powerpc/lib || exit 1

	report ""
	report "Linking powerpc '${libname}'..."

	lipo -create \
			ppc/lib/${libname} \
		-output \
			powerpc/lib/${libname} || exit 1
			
	cp -R ppc/include universal || exit 1

	report "Created powerpc '${libname}'..."
	report ""

	popd
}


####################################################################################################

